// pages/index/searchForSJ/searchForSJ.js
const app = getApp();
import HttpRequst from '../../../utils/httpRequst.js'
import API from '../../../utils/config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    systermInfo:"",
    scrollH:"",
    page:1,
    pageSize:20,
    searchVal:"",
    isEndData:false,
    searchList:[],
    isNoData:false
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var query = wx.createSelectorQuery();
    var that=this;
    this.setData({
      systermInfo: app.globalData.systemInfo
    })
    query.select('#J_sec-search-container').boundingClientRect(function (rect) {
      that.setData({
        scrollH: app.globalData.systemInfo.windowHeight - rect.height
      })
    }).exec();
  },

  getSearchValue: function (e){
    this.setData({
     
    })
    var keyWord = e.detail.value;  
    var that=this; 
    this.setData({
      page: 1,
      searchList:[],
      searchVal: e.detail.value
    })
    this.throttle(this.requestData, null, 250, keyWord);
  },

  clearkeyWord:function(){
    this.setData({
      searchVal:"",
      page:1,
      searchList:[]
    })
  },
  addShangJia:function(){
    wx.navigateTo({
      url: '/pages/index/shangJiaAddForm/shangJiaAddForm',
    })
    // if (!app.getUkey() || !app.getStoreUserInfo()){
    //   wx.showModal({
    //     title: '提示',
    //     content: '您未登录，请先登录',
    //     cancelText: '我知道了',
    //     confirmText: '立即登录',
    //     success: function (res) {
    //       if (res.confirm) {
    //         wx.navigateTo({
    //           url: '/pages/index/login/login',
    //         })
    //       } else if (res.cancel) {

    //       }
    //     }
    //   })
    // }else{
      
    // }
  },

  requestData: function (keyWord){
    var that=this;
    HttpRequst(false, API.bizs, 1, "", { keyword: keyWord}, "POST", false, function (res) {
      var nextPage = that.data.page + 1;
      
      that.setData({
        page: nextPage,
        searchList:res.data.bizs,
        isNoData: res.data.bizs.length ? false :true,
        isEndData: (res.data.bizs.length < that.data.pageSize) ? true :false
      })
    })
  },

  lower:function(e){
    var that=this;
    if (this.data.isEndData) return;
    HttpRequst(false, API.bizs, 1, "", { keyword: this.data.searchVal, page: this.data.page}, "POST", false, function (res) {
      
      that.setData({
        page: that.data.page+1,
        searchList: that.data.searchList.concat(res.data.bizs),
        isEndData: (res.data.bizs.length < that.data.pageSize) ? true : false
      })
    })
  },

  // 节流
  throttle: function (fn, context, delay, text) {
    clearTimeout(fn.timeoutId);
    fn.timeoutId = setTimeout(function () {
      fn.call(context, text);
    }, delay);
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})